package com.lagou;

import com.lagou.filter.TPMonitorFilter;
import com.lagou.filter.MethodRespone;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

public class PrintMethod {

    public void print(String method){
        System.out.println("开始打印");
        List<MethodRespone> methodResponeList = TPMonitorFilter.map.get(method);
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        if(methodResponeList != null && methodResponeList.size()>0){
            LocalDateTime end = LocalDateTime.now();
            LocalDateTime begin = end.minusMinutes(1);
            List<Long> list = methodResponeList.stream().filter((obj) -> {
                if (obj.getResponseTime().compareTo(begin) >= 0 && obj.getResponseTime().compareTo(end) <= 0) {
                    return true;
                }
                return false;
            }).map(MethodRespone::getMethodResponseTime).sorted().collect(Collectors.toList());
            Double tp90 = list.size() * 0.90;
            Double tp99 = list.size() * 0.99;
            Long TP90 = list.get(tp90.intValue());
            Long TP99 = list.get(tp99.intValue());
            System.out.println(method + " " + df.format(begin) + "到" + df.format(end) + "的TP90耗时情况：" + TP90);
            System.out.println(method + " " + df.format(begin) + "到" + df.format(end) + "的TP99耗时情况：" + TP99);
        }
    }
}
